home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / DJGPP / DJSRC111.ZIP / go32 / dalloc.c < prev    next >
C/C++ Source or Header  |  1993-10-09  |  4KB  |  180 lines

  1. /* This is file DALLOC.C */
  2. /*
  3. ** Copyright (C) 1993 DJ Delorie, 24 Kirsten Ave, Rochester NH 03867-2954
  4. **
  5. ** This file is distributed under the terms listed in the document
  6. ** "copying.dj", available from DJ Delorie at the address above.
  7. ** A copy of "copying.dj" should accompany this file; if not, a copy
  8. ** should be available from where this file was obtained.  This file
  9. ** may not be distributed without a verbatim copy of "copying.dj".
  10. **
  11. ** This file is distributed WITHOUT ANY WARRANTY; without even the implied
  12. ** warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. */
  14.  
  15. /* History:22,22 */
  16.  
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <dos.h>
  20. #include <dir.h>
  21. #include <io.h>
  22. #include <fcntl.h>
  23. #include <sys/stat.h>
  24. #include <string.h>
  25.  
  26. #include "gotypes.h"
  27. #include "valloc.h"
  28. #include "dalloc.h"
  29. #include "mono.h"
  30. #include "control.h"
  31.  
  32. #define DA_FREE    0
  33. #define DA_USED    1
  34.  
  35. #define MAX_DBLOCK 32760 /* 4095 * 8 -> 128 Mb */
  36.  
  37. static int dalloc_initted = 0;
  38. static word8 map[4096];
  39. static first_avail;
  40. static int dfile = -1;
  41. static word32 disk_used = 0;
  42.  
  43. extern int debug_mode;
  44.  
  45. static void dset(unsigned i, int b)
  46. {
  47.   unsigned o, m;
  48.   o = i>>3;
  49.   m = 1<<(i&7);
  50.   if (b)
  51.     map[o] |= m;
  52.   else
  53.     map[o] &= ~m;
  54. }
  55.  
  56. static int dtest(unsigned i)
  57. {
  58.   unsigned o, m;
  59.   o = i>>3;
  60.   m = 1<<(i&7);
  61.   return map[o] & m;
  62. }
  63.  
  64. static char dfilename[80];
  65.  
  66. void dalloc_init(void)
  67. {
  68.   int i;
  69.   char *tmp;
  70.   tmp = getenv("GO32TMP");
  71.   if (!tmp) tmp = getenv("GCCTMP");
  72.   if (!tmp) tmp = getenv("TMP");
  73.   if (!tmp) tmp = getenv("TEMP");
  74.   if (!tmp) tmp = "/";
  75.   if ((tmp[strlen(tmp)-1] == '/') || (tmp[strlen(tmp)-1] == '\\'))
  76.     sprintf(dfilename, "%spg%04xXXXXXX", tmp, _CS);
  77.   else
  78.     sprintf(dfilename, "%s/pg%04xXXXXXX", tmp, _CS);
  79.   for (i=0; i<4096; i++)
  80.     map[i] = 0;
  81.   first_avail = 0;
  82.   dalloc_initted = 1;
  83.   if (show_memory_info)
  84.   {
  85.     fprintf(stderr, "Swap space available: %ld Kb\n", dalloc_max_size() * 4L);
  86.   }
  87. }
  88.  
  89. void dalloc_uninit(void)
  90. {
  91.   if (dfile == -1)
  92.     return;
  93.   close(dfile);
  94.   unlink(dfilename);
  95. }
  96.  
  97. unsigned dalloc(void)
  98. {
  99.   char buf[8];
  100.   int i;
  101.   unsigned pn;
  102.   if (!dalloc_initted)
  103.     dalloc_init();
  104.   for (pn=first_avail; pn<=MAX_DBLOCK; pn++)
  105.     if (dtest(pn) == DA_FREE)
  106.     {
  107.       dset(pn, DA_USED);
  108.       first_avail = pn+1;
  109.       disk_used++;
  110.       if (topline_info)
  111.       {
  112.         sprintf(buf, "%6ldk", disk_used*4);
  113.         for (i=0; i<7; i++)
  114.           poke(screen_seg, (54+i)*2, buf[i]|0x0c00);
  115.       }
  116.       return pn;
  117.     }
  118.   fprintf(stderr, "Fatal: out of swap space!\n");
  119.   return 0;
  120. }
  121.  
  122. void dfree(unsigned pn)
  123. {
  124.   dset(pn, DA_FREE);
  125.   if (pn < first_avail)
  126.     first_avail = pn;
  127.   disk_used--;
  128. }
  129.  
  130. unsigned dalloc_max_size(void)
  131. {
  132.   word32 fr;
  133.   struct REGPACK r;
  134.   r.r_ax = 0x3600;
  135.   if (dfilename[1] == ':')
  136.     r.r_dx = dfilename[0] & 0x1f;
  137.   else
  138.     r.r_dx = 0;
  139.   intr(0x21, &r);
  140.   if (r.r_ax == 0xffff)
  141.     return 0;
  142.   fr = (word32)r.r_ax * (word32)r.r_bx * (word32)r.r_cx;
  143.   fr /= 4096L;
  144.   if (fr > MAX_DBLOCK)
  145.     fr = MAX_DBLOCK;
  146.   return (unsigned)fr;
  147. }
  148.  
  149. unsigned dalloc_used(void)
  150. {
  151.   return (unsigned)disk_used;
  152. }
  153.  
  154. void dwrite(word8 *buf, unsigned block)
  155. {
  156.   int c;
  157.   if(dfile < 0) {
  158.     mktemp(dfilename);
  159.     dfile = open(dfilename, O_RDWR|O_BINARY|O_CREAT|O_TRUNC, S_IWRITE|S_IREAD);
  160.     if (dfile < 0)
  161.     {
  162. fprintf(stderr, "Fatal! cannot open swap file %s\n", dfilename);
  163. exit(1);
  164.     }
  165.   }
  166.   lseek(dfile, (long)block*4096L, 0);
  167.   c = write(dfile, buf, 4096);
  168.   if (c < 4096)
  169.   {
  170.     fprintf(stderr, "Fatal! disk full writing to swap file\n");
  171.     exit(1);
  172.   }
  173. }
  174.  
  175. void dread(word8 *buf, unsigned block)
  176. {
  177.   lseek(dfile, (long)block*4096L, 0);
  178.   read(dfile, buf, 4096);
  179. }
  180.